<?php 
namespace app\home\controller;

use think\Db;
use think\Request;
use app\home\controller\Common;
use think\Validate;

/**
 * @title 个人中心
 * @group 樱桃荟
 */
class Usercenter extends Common
{
    /**
     * @title 个人资料
     * @url /home/Usercenter/user
     * @method GET
     * @return data:个人资料
     */
    public function user(Request $request)
    {
        $user=$request->user;
        return json($user);
	}
    /**
     * @title 修改个人资料
     * @url /home/Usercenter/user_edit
     * @method GET
     * @param name:user_apliy_name type:int require:1 other: desc:提现支付宝姓名
     * @param name:user_apliy_number type:string require:1 other: desc:提现支付宝账号
     * @param name:number_id type:string require:1 other: desc:身份证号
     * @return status:状态 0(成功) 1(失败)
     * @return msg:提示语
     */
    public function user_edit(Request $request)
    {
        $validate=Validate::make([
            'user_apliy_name'=>'require',
            'user_apliy_number'=>'require',
            'number_id'=>'require|min:18',
        ],[
            'user_apliy_name.require'=>'提现支付宝姓名不能为空',
            'user_apliy_number.require'=>'提现支付宝账号不能为空',
            'number_id.require'=>'身份证号不能为空',
            'number_id.min'=>'身份证号最少为18位'
        ]);
        $data=[
            'user_apliy_name'=>$request->param('user_apliy_name'),
            'user_apliy_number'=>$request->param('user_apliy_number'),
            'number_id'=>$request->param('number_id')
        ];
       $result=$validate->check($data);
       if(!$result){
           return json(['status'=>1,'msg'=>$validate->getError()]);
       }
       $uid=$request->user['user_id'];
       $up=Db::name('user')->where('user_id',$uid)->update($data);
       if($up===false){
           return json(['status'=>1,'msg'=>'修改成功']);
       }else{
           return json(['status'=>0,'msg'=>'修改失败']);
       }
	}

    /**
     *  @title 资金记录
     *  @author  田云川
     *  @description 响应数据 状态  时间  金额
     *  @method  GET
     *  @url /home/Usercenter/capitalRecord
     *  @return create_time:  充值时间
     *  @return amount:  充值金额
     *  @return status:  状态
     */
    public function capitalRecord(Request $request){
        $user = $request->user;
        $uid = $user['user_id'];
        $rechargeInfo =db('wallet_log')->where(['user_id'=>$uid,'type'=>2])->field('create_time,amount,status')->select();
        foreach ($rechargeInfo as $k=>$v){
            $rechargeInfo[$k]['amount'] = number_format($v['amount'],2);
            $rechargeInfo[$k]['status'] = $v['status']==0 ? '失败':'成功';
        }
        return json_success($rechargeInfo);
    }

    /**
     * @title 我的代理
     * @author 田云川
     * @description 响应数据 昵称  账号/推广数  注册时间
     * @method GET
     * @url /home/Usercenter/myAgent
     * @return user_name: 用户昵称
     * @return gen_number: 账号/推广人数
     * @return create_time: 时间
     */
    public function myAgent(Request $request){
        $user = $request->user;
        $uid = $user['user_id'];
        $agentInfo = db('user')->where(['user_parent'=>$uid])->select();
        foreach ($agentInfo as $k=>$v){
            $agentInfo[$k]['gen_number'] = $v['user_phone'].'('.db('user')->where(['user_parent'=>$v['user_id']])->count().')';
        }
        return json_success($agentInfo);
    }

    /**
     * @title 收入记录
     * @author 田云川
     * @method POST
     * @url /home/Usercenter/revenueRecord
     * @param name:page type:int require:1 default:1 other: desc:当前第几页
     * @param name:pageN type:int require:1 default:1 other: desc:每页条数
     * @return totalN:总条数
     * @return note:类型
     * @return money:金额
     * @return create_time:时间
     * **/
    public function revenueRecord(Request $request){
        $user = $request->user;
        $data = $request->post();
        $totalN = db('share_profit')->where(['user_id'=>$user['user_id']])->count();
        //计算偏移量
        $offset =  ($data['page'] - 1)* $data['pageN'];
        $income = db('share_profit')->where(['user_id'=>$user['user_id']])->field('note,money,create_time')->limit($offset,$data['pageN'])->select();
        return json_success($income,$totalN);
    }


}





